package com.rng.dateless;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.rng.dateless.R;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;

/**
 * Created by tkoh on 10.10.2014.
 */
public class ListActivity extends Activity{
    DBHelper dbHelper;
    ArrayList <Item> data;

    @Override
    public void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getActionBar().setDisplayHomeAsUpEnabled(true);
        setContentView(R.layout.list);
    }

    @Override
    public void onResume () {
        data = new ArrayList<Item>();
        super.onResume();
        String description;
        dbHelper = new DBHelper(this);
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Cursor c = db.query(DBHelper.TABLE_NAME, null, null, null, null, null, null);
        ListView listView = (ListView) findViewById(R.id.lvMain);
        if (c.moveToFirst()) {
            do {
                int dateName = c.getColumnIndex(DBHelper.COL_DATE);
                int descriptionIndex = c.getColumnIndex(DBHelper.COL_DESCRIPTION);
                description = (c.getString(descriptionIndex));
                Calendar calendar = new GregorianCalendar();
                try {
                    calendar.setTime(dateFormat.parse(c.getString(dateName)));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                data.add(new Item(calendar.get(Calendar.DAY_OF_MONTH), calendar.get(Calendar.MONTH), calendar.get(Calendar.YEAR), description, "кожного року"));
            }while (c.moveToNext());
        }
        c.close();
        db.close();
        CustomAdapter adapter = new CustomAdapter(this,R.layout.list_row, data.toArray(new Item[data.size()]));
        listView.setAdapter(adapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch (id) {
            case R.id.action_new:
                Intent intent = new Intent(getApplicationContext(), NewEvent.class);
                startActivity(intent);
                return true;
            case android.R.id.home:
                NavUtils.navigateUpFromSameTask(this);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    private static class CustomAdapter extends ArrayAdapter<Item> {
        public CustomAdapter(Context context, int layout,
                             Item[] items) {
//Call through to ArrayAdapter implementation
            super(context, layout, items);
        }
        @Override
        public View getView(int position, View convertView,
                            ViewGroup parent) {
            View row = convertView;
//Inflate a new row if one isn't recycled
            if(row == null) {
                row = LayoutInflater.from(getContext())
                        .inflate(R.layout.list_row, parent, false);
            }
            TextView day = (TextView)row.findViewById(R.id.day);
            TextView month = (TextView)row.findViewById(R.id.month);
            TextView year = (TextView)row.findViewById(R.id.year);
            TextView description = (TextView)row.findViewById(R.id.description);
            TextView details = (TextView)row.findViewById(R.id.details);

            day.setText(Integer.toString(getItem(position).getDay()));
            month.setText(Integer.toString(getItem(position).getMonth()));
            year.setText(Integer.toString(getItem(position).getYear()));
            description.setText(getItem(position).getDescription());
            details.setText(getItem(position).getDetails());
            return row;
        }
    }
}
